#include <iostream>

using namespace std;

const int N = 2e6 + 10;

int n, h;
int ne[N];

int main()
{
	cin >> n;
	for(int i = 1; i <= n; i++) cin >> ne[i];
	cin >> h;

	//遍历链表
	int tmp = h;
	while(tmp)
	{
		cout << tmp << " ";
		tmp = ne[tmp];
	}
	cout << endl;

	return 0;
}